<?php
class Logger
{
    private $handle;
    private $level = 0;
    private $file;
    private $date;
    const DEBUG = 0;
    const INFO = 1; 
    const ERROR = 2;

    public function __construct($config){
        $this->level = 1;
        $this->file = $this->filename();
        if (!is_dir(dirname($this->file))){ mkdir(dirname($this->file)); }
        $this->handle = fopen($this->file, "a");
    }
    private function reinit(){
        if (!is_file($this->filename())){
            $this->file = $this->filename();
            $this->handle = fopen($this->file, "a");
        }
    }

    private function filename(){ return 'logs\log-'.date('mdy').'.txt'; }

    private function rotate(){
        if ($this->date == date('mdy')){
            if(is_file("$this->file".'1')) {
                unlink("$this->file".'1');    
            }
        } 
    }

    private function write($m){
        $this->reinit();
        if ($this->handle){
            $m = date('m-d-y h:i:sA :') . $m . "\r\n";
            if(!fwrite($this->handle, $m)) return FALSE; 
        }
    } 
    public function debug($m){
        if ($this->level < 1) $this->write("DEBUG: ".$m);
    }
    
    public function info($m){
        if ($this->level < 2) $this->write("INFO: ".$m);
    }

    public function error($m){
        if ($this->level < 3) $this->write("ERROR: ".$m);
    
    }

}
?>
